//
//  KPHomeController.swift
//  KPUserCenter
//
//  Created by Daiyi on 2024/4/16.
//

import UIKit
import DDBase
import KPAppKit

public class KPHomeController: DDTableViewController<KPOrderModel> {

    public override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationItem.title = nil
        
        
        DispatchQueue.main.async { [weak self] in
            self?.refreshData()
        }
    }
    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
    }
    
    public override func createUI() {
        super.createUI()
        self.view.addSubview(self.navigationBar)
        
        let tabBgView = UIImageView(frame: UIScreen.main.bounds)
        if let filePath = Bundle.homeBundle.path(forResource: "ic_home_bg", ofType: "png") {
            tabBgView.image = UIImage(contentsOfFile: filePath)
        }
        
        self.tableView.backgroundView = tabBgView
        self.tableView.contentInset = UIEdgeInsets(top: 44, left: 0, bottom: DDUtils.safe.bottom, right: 0)
        
        self.tableView.tableHeaderView = self.headerView
        
        self.lendingView.isHidden = false

        
    }
    

    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    private func refreshData() {
        self.dd.showLoading()
        self.viewModel.loadHeaderData()
    }
    
    public override func loadComplete(_ vmModel: Any, code: Int, message: String?) {
        self.tableView.mj_header?.endRefreshing()
        self.dd.dismissTips()
        if let msg = message, msg.count > 0 {
            self.dd.showTip(msg)
        }
    }
    
    public override func buildViewModel() -> DDBaseViewModel<KPOrderModel> {
        return KPHomeViewModel(DDNetworkManager.provider, delegate: self)
    }
    
    public override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.tableView {
            let scrollTop = (scrollView.contentOffset.y + self.navigationBar.height)
            let alpha = scrollTop > 44 ? 1.0 :  scrollTop / 44
            self.navigationBar.backgroundColor = .white.withAlphaComponent(alpha)
        }
    }
    
    // MARK: - getter And setter ======================================
    private lazy var navigationBar: UIView = {
        let logoview = UIView(frame: CGRect(x: 0, y: 0, width: DDUtils.SCREEN_W, height: 44 + DDUtils.safe.top))
            
        let centerY = logoview.height - 22
        
        let logo = UIImageView(image: UIImage(hNamed: "ic_home_logo"))
            .ddChain
            .setMidY(centerY)
            .setX(6)
            .build()
        logoview.addSubview(logo)
        
        let set = UIButton(type: .custom).ddChain
            .frame(CGRect(x: logoview.width - 56, y: DDUtils.safe.top, width: 56, height: 44))
            .setImage(UIImage(hNamed: "ic_home_setting"))
            .addTarget(self, action: #selector(goSetting))
            .build()
        logoview.addSubview(set)
        
        logoview.backgroundColor = .white.withAlphaComponent(0)
        
        return logoview
    }()
    private lazy var headerView: UIView = {
        let header = UIView(frame: CGRect(x: 0, y: 0, width: DDUtils.SCREEN_W, height: 14 + 240 + 24 + 60 + 12))
        
        let color = UIColor.black.withAlphaComponent(0.6)
        let loanTip = UIButton(type: .custom).ddChain
            .frame(CGRect(x: 16, y: header.height - 12 - 60, width: header.width - 32, height: 60))
            .font(.font12)
            .setTitleColor(color)
            .backgroundColor(.white)
            .contentInset(UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16))
            .build()
        loanTip.contentHorizontalAlignment = .leading
        loanTip.dd.makeRadius(10)
        loanTip.titleLabel?.numberOfLines = 0
        header.addSubview(loanTip)
        
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 4
        style.headIndent = 6
        let attr = NSMutableAttributedString(string: "· Solo pago a través de la APP\n· Mantén la App sin desinstalar, al volver a solicitar obtendrás un mayor monto de préstamo.", attributes: [.paragraphStyle: style, .foregroundColor: color, .font: UIFont.font12])
        loanTip.setAttributedTitle(attr, for: .normal)
        
        return header
    }()
//    
//    private lazy var firstLoanView: SHFirstLoaningView = {
//        let loanView = SHFirstLoaningView { [weak self] loanView in
//            guard let self = self else {return}
//            self.goLoan()
//        }
//        loanView.isHidden = true
//        return loanView
//    }()

    private lazy var lendingView: KPLendingView = {
        let lendingView = KPLendingView(frame: CGRect(x: 16, y: 14, width: DDUtils.SCREEN_W - 32, height: 240)) { [weak self] loanView in
            guard let self = self else {return}
//            self.goAhead()
        }
        lendingView.isHidden = true
        lendingView.backgroundColor = .white
        lendingView.dd.makeRadius(10)
        self.headerView.addSubview(lendingView)
        lendingView.isHidden = true
        return lendingView
    }()
}

extension KPHomeController {
    @objc private func goSetting() {
        Log.d("geSetting")
        let vc = KPOrderConfirmController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
